package com.ruoyi.project.medicinal.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.medicinal.domain.StockProducter;
import com.ruoyi.project.medicinal.service.ProducterService;
import com.ruoyi.project.system.domain.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/med/pro")
public class ProducterController extends BaseController {
    @Autowired
    private ProducterService producterService;

    /**
     * 获取工厂列表
     */
    @PreAuthorize("@ss.hasPermi('med:pro:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockProducter producter) {
        startPage();
        List<StockProducter> list = producterService.selectProList(producter);
        return getDataTable(list);
    }

    /**
     * 新增厂家
     */
    @PreAuthorize("@ss.hasPermi('med:pro:add')")
    @Log(title = "厂家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StockProducter producter) {
        producter.setCreateBy(SecurityUtils.getUsername());
        return toAjax(producterService.insertPro(producter));
    }

    /**
     * 根据厂家编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('med:pro:query')")
    @GetMapping(value = "/{producterId}")
    public AjaxResult getInfo(@PathVariable Long producterId) {
        return AjaxResult.success(producterService.selectProById(producterId));
    }

    /**
     * 修改厂家
     */
    @PreAuthorize("@ss.hasPermi('med:pro:edit')")
    @Log(title = "厂家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StockProducter producter)
    {
        producter.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(producterService.updatePro(producter));
    }

    /**
     * 批量删除工厂
     */
    @PreAuthorize("@ss.hasPermi('med:pro:remove')")
    @Log(title = "工厂管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{producterId}")
    public AjaxResult remove(@PathVariable Long[] producterId) {
        return toAjax(producterService.deleteProByIds(producterId));
    }

}
